package com.example.forquestion;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.http.util.EncodingUtils;

import com.example.forquestion.adapter.MyListAdapter;
import com.example.forquestion.askquestion.Askquestion;
import com.example.forquestion.custonListView.CustomListView;
import com.example.forquestion.custonListView.CustomListView.OnLoadMoreListener;
import com.example.forquestion.custonListView.CustomListView.OnRefreshListener;
import com.example.forquestion.domain.FirendMicroList;
import com.example.forquestion.domain.FirendMicroListDatas;
import com.example.forquestion.domain.FirendsMicro;
import com.example.forquestion.util.FastjsonUtil;
import com.example.xuetang.HttpUtils;
import com.example.xuetang.R;

import android.os.Bundle;
import android.os.IBinder;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources.NotFoundException;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;

public class Wenti extends Activity {
	private ImageButton ask;

	private static final String TAG = "MicroActivity";
	private String uid="";
	private String companykey="";
	int now = 0;
	int count = 0;
	private String strIcon="";//存放头像路径
	List<FirendMicroListDatas> listdatas=new ArrayList<FirendMicroListDatas>();//json数据
	private View header;
	public CustomListView listview;
	public MyListAdapter mAdapter;//这是真正的
	private ImageView MicroIcon;
	String res="";//json数据

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main_wenti);
		
		
		try {
			String path_question = "http://1.teachapplication.sinaapp.com/servlet/LoadMessageAction";
		    res = HttpUtils.liebiao(path_question);
		} catch (Exception e) {
			e.printStackTrace();
		} 
		
		init();
		askquestion();
	}
	
	private void askquestion() {
		ask = (ImageButton)findViewById(R.id.ask);
		ask.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent askquestions = new Intent();
				askquestions.setClass(Wenti.this, Askquestion.class);
				startActivity(askquestions);
			}
		});
	}

	private void init() {
		header=LayoutInflater.from(this).inflate(R.layout.micro_list_header_question, null);
		MicroIcon=(ImageView) header.findViewById(R.id.MicroIcon);
		listview=(CustomListView) findViewById(R.id.list);
		listview.setVerticalScrollBarEnabled(false);
		listview.setDivider(null);
		listview.addHeaderView(header);
		listview.setAdapter(mAdapter);
//		flag=false;
//		mAdapter = new MyListAdapter(this, listdatas,flag);
		mAdapter = new MyListAdapter(this, listdatas);
		listview.setAdapter(mAdapter);
		
		getMicroList(0, true);
//		new MyTask().execute();
		 //下拉刷新
		listview.setOnRefreshListener(new OnRefreshListener() {
			
			@Override
			public void onRefresh() {
				// TODO Auto-generated method stub
				String s="下拉刷新";
				getData(s);
			}
			
		});
  	  //上拉加载更多
		listview.setOnLoadListener(new OnLoadMoreListener() {

			public void onLoadMore() {
				String s="上拉加载更多";
				getData(s);
				
			}
		});
	}
	
	/**
	 * 得到主界面的列表
	 * @param i
	 * @param has
	 */
	private void getMicroList(final int i, boolean has) {
		if(TextUtils.isEmpty(res)){
			return;	
		}
		
		FirendsMicro fm=FastjsonUtil.json2object(res, FirendsMicro.class);
		FirendMicroList fList=fm.getFriendPager();
		
		if(true){
			
			if(i==0){
				listdatas.clear();
				count=0;
			}
			//一般来说我们会开另一个线程去获取数据，所以这儿会加上一个判断，如果已经在获取数据了，
			//就onRefreshComplete()，就是将下拉收起；否则就去开新线程取数据，取完记得也要onRefreshComplete()
			if(null ==fList.getDatas() || fList.getDatas().size()==0){
				if(i==0){
					listview.onRefreshComplete();
				}else{
					listview.onLoadMoreComplete(false);
				}
			}else{
				if(i==0){
					listview.onRefreshComplete();
				}else{
					listview.onLoadMoreComplete();
				}
				listdatas.addAll(fList.getDatas());
				count++;
			}
			int k=listdatas.size();
			now=k>0?k-1:0;
			mAdapter.notifyDataSetChanged();
		}
	}
	
	private void getData(String s) {
		// TODO Auto-generated method stub
		if("下拉刷新".equals(s)){
			
			getMicroList(0, true);
			
			listview.onRefreshComplete();
		}else{
			getMicroList(now, true);
			
			listview.onLoadMoreComplete(); // 加载更多完成
		}
	}
	
	public void LoadList(){
		getMicroList(0, true);
	}
}
